/*
 * This file is part of gshot.
 *
 * Gshot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Gshot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gshot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright (C) 2007-2011, Victor Ananjevsky <ananasik@gmail.com>
 */

#include "gshot.h"
#include "callback.h"
#include "tray.h"

static GtkStatusIcon *tray_icon = NULL;

static void on_activate (GtkStatusIcon *i, gpointer d)
{
  GtkWidget *win = GTK_WIDGET (gtk_builder_get_object (data.xml, "dialog"));

  if (gtk_widget_get_visible (win))
    gtk_widget_hide (win);
  else
    gtk_widget_show (win);
}

static void popup_tray_menu (GtkStatusIcon *i, guint b, guint t, gpointer d)
{
  GtkMenu *menu;
  GtkWidget *it, *om;

  menu = GTK_MENU (gtk_builder_get_object (data.xml, "tray_menu"));

  /* attach open submenu */
  it = GTK_WIDGET (gtk_builder_get_object (data.xml, "tray_open"));
  om = create_open_menu ();
  gtk_menu_item_set_submenu (GTK_MENU_ITEM (it), om);

  gtk_menu_popup (menu, NULL, NULL, gtk_status_icon_position_menu, i, b, t);
}

void show_tray ()
{
  if (!tray_icon)
    {
      tray_icon = gtk_status_icon_new_from_icon_name ("applets-screenshooter");
      gtk_status_icon_set_tooltip (tray_icon, _("Make a screenshot"));
      g_signal_connect (G_OBJECT (tray_icon), "activate",
			G_CALLBACK (on_activate), NULL);
      g_signal_connect (G_OBJECT (tray_icon), "popup-menu",
			G_CALLBACK (popup_tray_menu), NULL);
    }
}

void hide_tray ()
{
  gtk_status_icon_set_visible (tray_icon, FALSE);
  g_object_unref (tray_icon);
  tray_icon = NULL;
}

